package com.schrodinger.zbdp.licensegen.util;

import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class DesXmlReader {
	public static Map xmlReader(String xmlStr) throws Exception {
		ByteArrayInputStream stream;
		try {
			stream = new ByteArrayInputStream(xmlStr.getBytes());
			SAXReader reader = new SAXReader();
			Document doc = reader.read(stream);
			Element root = doc.getRootElement();

			Map desMap = new HashMap();
			for (Iterator iter = root.elementIterator("des"); iter.hasNext();) {
				License license = new License();
				Element foo = (Element) iter.next();
				String name = foo.elementText("name");
				String value = foo.elementText("value");
				String method = foo.elementText("method");
				license.setName(name);
				license.setValue(value);
				license.setMethod(method);
				desMap.put(name, license);
			}
			return desMap;
		} catch (DocumentException e) {
			throw new Exception(e);
		}
	}

	public static void main(String[] args) {
		String dd = "<xml><des><name>a</name><value>aaa</value><method>aaaaaa</method></des><des><name>b</name><value>bbb</value><method>bbbbbbb</method></des><des><name>c</name><value>cccc</value><method>ccccccc</method></des></xml>";
		try {
			xmlReader(dd);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}